<template>
    <h2>学生列表</h2>
    <div>
        <table width="800" border="1">
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>年级</th>
                <th>性别</th>
                <th>出生日期</th>
                <th>操作</th>
            </tr>
            <tr v-for="item in students" :key="item.studentNo">
                <td>{{ item.studentNo }}</td>
                <td>{{ item.studentName }}</td>
                <td>{{ item.gradeId }}</td>
                <td>{{ item.sex }}</td>
                <td>{{ item.bornDate }}</td>
                <td>
                    <button @click="loadOneStudent(item.studentNo)">修改</button>
                    <RouterLink :to="'/student/update/' + item.studentNo">修改</RouterLink>
                    <a-button type="primary" @click="handleDelete(item)">删除</a-button>
                </td>
            </tr>
        </table>
    </div>
</template>
<script>
import axios from 'axios';
export default {
    data() {
        return {
            students: [],
            grades: [],
            student: {
                studentNo: "",
                loginPwd: "",
                studentName: "",
                sex: "男",
                gradeId: 0,
                phone: "",
                address: "",
                bornDate: ""
            }
        }
    },
    methods: {
        loadstudents() {
            axios.get('/student/all')
                .then(res => {
                    this.students = res.data.data
                })
        },
        loadOneStudent(studentNo){
            this.$router.push({
                name:'studentupdate',
                params:{
                    id:studentNo
                }
            })
        },
        loadgrades() {
            axios.get('/grade/all',).then(res => {
                if (res.data.success) {
                    this.grades = res.data.data
                }
            })
        },
        handleDelete(student) {
            let flag = confirm(`确认要删除学号:${student.studentNo} 姓名:${student.studentName} 同学吗？`)
            if (flag) {
                axios.delete('/student/' + student.studentNo)
                    .then(res => {
                        if (res.data.success) {
                            this.loadstudents()
                            alert('删除成功')
                        }
                    })
            }
        }
    },
    created() {
        this.loadstudents()
        this.loadgrades()
    }
}
</script>
<style scoped></style>